/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
using namespace std;
#include "taxiStand.h"
#include <iostream>
#include <vector>
#include <cstdlib>

#include "modelentity.h"
#include "taxiArrivalInterpreter.h"
#include "customerArrivalInterpreter.h"
#include "taxtArrival.h"
#include "CustomerArrival.h"
#include "help.cpp"
#include "../tools/log.h"



void TaxiStand::execute(TaxiArrival* message)
{
if (_nrCustomers>0)
{
      _nrCustomers--;
	FILE_LOG(logINFO)<<"["<<message->getTimestamp()<<"]"
		<<"Taxi arrived and picked up customer at ME of LP"
		<<getAssocLp()->getId()<<endl;
	FILE_LOG(logINFO)<<"["<<message->getTimestamp()<<"]"
		<<"Taxis remaining: "<<_nrTaxis
		<<" / Customers remaining: "<<_nrCustomers<<endl;

	/*Taxi returns to random neighbor*/
	unsigned int nextstop=getNeighbors()[rand()%getNeighbors().size()];
	/*time needed to reach target taxistand
	 * normal Distribution */	
	randn ran;
	double gaus=ran.notrig();	
	double ts=NORMAL_DIST_MU+NORMAL_DIST_SIGMA*gaus+getAssocLp()->getLvt();
	if (ts<getAssocLp()->getLvt()+1.0)
	    ts=getAssocLp()->getLvt()+1.0; //Unlikely, but possible

	/* Send Taxi back */
	TaxiArrival* newTaxi=new TaxiArrival(ts,nextstop);
	getAssocLp()->sendMessage(newTaxi);
}
else
{
	_nrTaxis++;
	FILE_LOG(logINFO)<<"["<<message->getTimestamp()<<"]"
		<<"Taxi arrived at ME of LP "+getAssocLp()->getId()<<endl;
	FILE_LOG(logINFO)<<"["<<message->getTimestamp()<<"]"
		<<"Taxis remaining: "<<_nrTaxis
		<<" / Customers remaining: "<<_nrCustomers<<endl;



}

}

void TaxiStand::execute(CustomerArrival* message)
{
if (_nrTaxis>0)
{
      _nrTaxis--;
	FILE_LOG(logINFO)<<"["<<message->getTimestamp()<<"]"
		<<"Customer arrived and took taxi at ME of LP"
		<<getAssocLp()->getId()<<endl;
	FILE_LOG(logINFO)<<"["<<message->getTimestamp()<<"]"
		<<"Taxis remaining: "<<_nrTaxis
		<<" / Customers remaining: "<<_nrCustomers<<endl;

	/*Taxi returns to random neighbor*/
	unsigned int nextstop=getNeighbors()[rand()%getNeighbors().size()];
	/*time needed to reach target taxistand
	 * normal Distribution */	
	randn ran;
	double gaus=ran.notrig();	
	double ts=NORMAL_DIST_MU+NORMAL_DIST_SIGMA*gaus+getAssocLp()->getLvt();
	if (ts<getAssocLp()->getLvt()+1.0)
	    ts=getAssocLp()->getLvt()+1.0; //Unlikely, but possible

	/* Send Taxi back */
	TaxiArrival* newTaxi=new TaxiArrival(ts,nextstop);
	getAssocLp()->sendMessage(newTaxi);
}
else
{
	_nrCustomers++;
	FILE_LOG(logINFO)<<"["<<message->getTimestamp()<<"]"
		<<"Customer arrived at ME of LP "+getAssocLp()->getId()<<endl;
	FILE_LOG(logINFO)<<"["<<message->getTimestamp()<<"]"
		<<"Taxis remaining: "<<_nrTaxis
		<<" / Customers remaining: "<<_nrCustomers<<endl;



}
}

/**
 * Get the number of waiting customers
 * @return nrCustomer The number of waiting customers
 */
unsigned int TaxiStand::getNrCustomers()
{
return _nrCustomers;
}
/**
 * Get the number of waiting taxis
 * @return nrTaxis The number of waiting taxis
 */
unsigned int TaxiStand::getNrTaxis()
{
return _nrTaxis;
}
/**
 * Set the number of waiting customers
 * @param nrCustomers nrCustomers The number of customers
 */
void TaxiStand::setNrCustomers(unsigned int nrCustomers)
{
_nrCustomers=nrCustomers;
}

/**
 * Set the number of waiting taxis
 * @param nrTaxis The number of taxis
 */
void TaxiStand::setNrTaxis(unsigned int nrTaxis)
{
_nrTaxis=nrTaxis;
}

